﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Nova_Common;
using Nova_BLL;
using Nova_DAL;
using Nova_MOD;

public partial class System_Menu_MenuEdit : System.Web.UI.Page
{
    DataSet ds = new DataSet();
    Nova_MOD.Menu Nova_MOD_Menu = new Nova_MOD.Menu();
    Nova_BLL.Menu Nova_BLL_Menu = new Nova_BLL.Menu();
    protected int iCount = 0;
    protected string strType = "add";//传值 类型 add：添加；edit：修改；
    protected int iID = 0;//传值 id

    protected void Page_Load(object sender, EventArgs e)
    {
        LoginLogic.IsLogin();

        if (Request["Type"] != null && Request["Type"].ToString() != "")
        {
            strType = "edit";
        }
        if (Request["ID"] != null && Request["ID"].ToString() != "")
        {
            iID = Convert.ToInt32(Request["ID"].ToString());
        }

        if (!IsPostBack)
        {
            if (strType == "edit")
            {
                bind();
            }
        }

        //验证TextBox
        string strJs = "";
        strJs = "return Validate(";
        strJs += "'txtiNode','结点不为空','r_num'";
        strJs += ",'txtiParent','父结点不为空','r_num'";
        strJs += ",'txtvText','菜单名称不为空','r'";
        strJs += ");";
        ibtnSubmit.Attributes.Add("onclick", strJs);
    }

    protected void bind()
    {
        ds = Nova_BLL_Menu.DataListMenu(iID);
        txtiNode.Text = ds.Tables[0].Rows[0]["iNode"].ToString();
        txtiParent.Text = ds.Tables[0].Rows[0]["iParent"].ToString();
        txtvText.Text = ds.Tables[0].Rows[0]["vText"].ToString();
        txtvUrl.Text = ds.Tables[0].Rows[0]["vUrl"].ToString();
        txtvTarget.Text = ds.Tables[0].Rows[0]["vTarget"].ToString();
        txtvMenuNo.Text = ds.Tables[0].Rows[0]["vMenuNo"].ToString();
        txtiOrder.Text = ds.Tables[0].Rows[0]["iOrder"].ToString();
    }

    /// <summary>
    /// 确定按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnSubmit_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            Nova_MOD_Menu.id = iID;
            Nova_MOD_Menu.iNode = Convert.ToInt32(txtiNode.Text.Trim());
            Nova_MOD_Menu.iParent = Convert.ToInt32(txtiParent.Text.Trim());
            Nova_MOD_Menu.vText = txtvText.Text.Trim();
            Nova_MOD_Menu.vUrl = txtvUrl.Text.Trim();
            Nova_MOD_Menu.vTarget = txtvTarget.Text.Trim();
            Nova_MOD_Menu.vImg = "";//暂定
            Nova_MOD_Menu.vMenuNo = txtvMenuNo.Text.Trim();
            Nova_MOD_Menu.iOrder = Convert.ToInt32(txtiOrder.Text.Trim());

            if (strType == "add")
            {
                iCount = Nova_BLL_Menu.AddMenu(Nova_MOD_Menu);
            }
            else
            {
                iCount = Nova_BLL_Menu.UpdateMenu(Nova_MOD_Menu);
            }
            if (iCount > 0)
            {
                ShowBox.MessageBox(this.Page, "操作成功！", "MenuList.aspx");              
                return;
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }

    /// <summary>
    /// 返回上一级
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnReset_Click(object sender, ImageClickEventArgs e)
    {
        if (strType == "add")
        {
            txtiNode.Text = "0";
            txtiParent.Text ="0";
            txtvText.Text = "";
            txtvUrl.Text = "";
            txtvTarget.Text = "main";
            txtvMenuNo.Text = "";
            txtiOrder.Text = "0";
        }
        else
        {
            bind();
        }
    }
}
